package com.example.friend.controller.user;

import com.example.common.core.Controller.BaseController;
import com.example.common.core.constants.HttpConstants;
import com.example.common.core.domin.R;
import com.example.common.core.domin.vo.LoginUserVO;
import com.example.friend.domain.user.dto.UserDTO;
import com.example.friend.domain.user.dto.UserUpdateDTO;
import com.example.friend.domain.user.vo.UserVO;
import com.example.friend.service.user.IUserService;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
@Tag(name = "C端用户功能接口")
public class UserController extends BaseController {

    @Autowired
    private IUserService userService;

    //获取验证码
    //接口地址: /user/sendCode
    @PostMapping("/sendCode")
    public R<Void> sendConde(@RequestBody UserDTO userDTO) {
        return toR(userService.sendCode(userDTO));
    }

    //登录注册
    //接口地址: /user/code/login
    @PostMapping("/code/login")
    public R<String> codeLogin(@RequestBody UserDTO userDTO) {
        return R.ok(userService.codeLogin(userDTO.getCode(),userDTO.getPhone()));
    }

    //退出登录
    //接口地址: /user/logout
    //参数: token信息,直接放到请求头里面
    @DeleteMapping("/logout")
    public R<Void> logout(@RequestHeader(HttpConstants.AUTHENTICATION) String token) {
        return toR(userService.logout(token));
    }

    //获取当前用户登录信息
    //接口地址: /user/info
    @GetMapping("/info")
    public R<LoginUserVO> info(@RequestHeader(HttpConstants.AUTHENTICATION) String token) {
        return userService.info(token);
    }

    //获取个人信息
    //接口地址: /user/detail
    @GetMapping("/detail")
    public R<UserVO> detail() {
        return R.ok(userService.detail());
    }

    //编辑用户信息
    //接口地址: /user/edit
    @PutMapping("/edit")
    public R<Void> edit(@RequestBody UserUpdateDTO userUpdateDTO) {
        return toR(userService.edit(userUpdateDTO));
    }

    //更新用户头像
    @PutMapping("/head-image/update")
    public R<Void> updateHeadImage(@RequestBody UserUpdateDTO userUpdateDTO) {
        return toR(userService.updateHeadImage(userUpdateDTO.getHeadImage()));
    }
}
